package com.example.mydemo.config;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.resource.PathResourceResolver;
import org.springframework.web.servlet.resource.ResourceResolverChain;

import java.util.List;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    RequestLogInterceptor requestLogInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        // 确保Swagger UI路径不被Thymeleaf拦截
        registry.addViewController("/swagger-ui.html").setViewName("forward:/swagger-ui.html");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc-openapi-ui/")
                .resourceChain(true)
                .addResolver(new PathResourceResolver() {
                    @Override
                    protected Resource resolveResourceInternal(
                            HttpServletRequest request,
                            String requestPath,
                            List<? extends Resource> locations,
                            ResourceResolverChain chain) {
                        return chain.resolveResource(request, requestPath, locations);
                    }
                });
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/lib/**").addResourceLocations("classpath:/lib/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

     System.out.println("WebConfig addInterceptors");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/static/css/**")//排除前端静态资源
                .excludePathPatterns("/static/js/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/api/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/static/api/**")
                .excludePathPatterns("/templates/**")
                .excludePathPatterns("/captcha")
                .excludePathPatterns("/debug")
//                .excludePathPatterns("/*.html")
                .excludePathPatterns("/login", "/logout","/adduser");
        registry.addInterceptor(requestLogInterceptor)
                .excludePathPatterns("/static/css/**")//排除前端静态资源
                .excludePathPatterns("/static/js/**")
                .excludePathPatterns("/pic/**")
                .excludePathPatterns("/lib/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/static/api/**")
                .excludePathPatterns("/templates/**")
                .excludePathPatterns("/log/**");
//                .excludePathPatterns("/**/*.html");
    }
}